package com.wkbb.sports.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.sports.dto.ProjectCategoryDto;
import com.wkbb.sports.mapper.HlifeProjectMapper;
import com.wkbb.sports.model.HlifeProjectCategory;
import com.wkbb.sports.model.HlifeProjectContent;
import com.wkbb.sports.service.ProjectService;
import com.wkbb.sports.utils.BeanBind;

@Service
public class ProjectServiceImpl extends BaseServiceImpl<ProjectCategoryDto> implements ProjectService {
	
	 @Autowired
	 private HlifeProjectMapper projectMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addProject(ProjectCategoryDto projectCategoryDto) throws Exception {
		
		BeanBind bind = new BeanBind(ProjectCategoryDto.class, HlifeProjectCategory.class);
		HlifeProjectCategory catory = (HlifeProjectCategory)bind.bindBean(projectCategoryDto);
		
		if(null ==catory.getId()){
			
			projectMapper.insert(catory);
			
			
		}else{
			projectMapper.update(catory);
			
			
		}
		
		List<HlifeProjectContent> addlist = new ArrayList<>();
		
		List<HlifeProjectContent> updlist = new ArrayList<>();
		
		if(CollectionUtils.isNotEmpty(catory.getConList())){
			
			Iterator it = catory.getConList().iterator();
			
			while(it.hasNext()){
				
				HlifeProjectContent hlifeProjectContent = (HlifeProjectContent) it.next();
				if(null != hlifeProjectContent.getProjectCategoryId()){
					updlist.add(hlifeProjectContent);
				}else{
					hlifeProjectContent.setProjectCategoryId(catory.getId());
					addlist.add(hlifeProjectContent);
				}
				
			}
			
		}
		
		if(CollectionUtils.isNotEmpty(addlist)){
			
			projectMapper.insertContsBatch(addlist);
		}
		
		if(CollectionUtils.isNotEmpty(updlist)){
			projectMapper.updateContsBatch(updlist);
		}
		
		
		
	}

	@Override
	public HlifeProjectCategory selProject(Long id) throws Exception {
		
		return  projectMapper.getByPrimaryKey(id);
		
		
	}

	@Override
	public List<HlifeProjectCategory> selProjectList() {
		
		return projectMapper.getProjectList();
	}

	

    
}
